require "nvchad.mappings"

function vim.getVisualSelection()
  local current_clipboard_content = vim.fn.getreg '"'

  vim.cmd 'noau normal! "vy"'
  local text = vim.fn.getreg "v"
  vim.fn.setreg("v", {})

  vim.fn.setreg('"', current_clipboard_content)

  text = string.gsub(text, "\n", "")
  if #text > 0 then
    return text
  else
    return ""
  end
end

-- 定义函数来搜索光标下的单词
function vim.live_grep_word_under_cursor()
  -- <cword> 是 Neovim 特殊的表达式，表示光标下的单词
  local word = vim.fn.expand "<cword>"

  if word == "" then
    print "No word found under cursor."
    return
  end

  -- 使用 \V (very nomagic) 和 \b (word boundary) 进行精确搜索
  require("telescope.builtin").grep_string {
    search = word,
    -- search = "\\V" .. word .. "\\b",
  }
end

-- unset nvchad internal map
vim.keymap.del("n", "<leader>ds")
vim.keymap.del("n", "<leader>gt")
vim.keymap.del("n", "<leader>th")
vim.keymap.del("n", "<leader>n")
vim.keymap.del("n", "<leader>rn")
vim.keymap.del("n", "<leader>ma")
vim.keymap.del("n", "<leader>cm")
vim.keymap.del("n", "<leader>h")
vim.keymap.del("n", "<leader>v")

-- set
local map = vim.keymap.set

map("n", ";", ":", { desc = "CMD enter command mode" })
map("i", "jk", "<ESC>")

-- which-key
local wk = require "which-key"
wk.add {
  { ",", group = "self" },
  { ",,", ",", desc = "self Prev ftFT" },
  { ",x", ":q<cr>", desc = "self close window" },
  { ",s", ":w<cr>", desc = "self save buffer" },
  {
    ",d",
    function()
      require("nvchad.tabufline").close_buffer()
    end,
    desc = "self close buffer",
  },
}

-- general
wk.add {
  { "<bs>", ":bp<cr>", desc = "buffer Prev buffer" },
  { "<leader>gn", "<cmd>set nu!<cr>", desc = "general toggle number" },
  { "<leader>gN", "<cmd>set rnu!<cr>", desc = "general toggle relative number" },
  { "<leader>h", "<c-w>h", desc = "window Move left", mode = "n" },
  { "<leader>l", "<c-w>l", desc = "window Move right", mode = "n" },
  { "<leader>j", "<c-w>j", desc = "window Move down", mode = "n" },
  { "<leader>k", "<c-w>k", desc = "window Move up", mode = "n" },
}

-- Telescope

wk.add {
  { "<leader>fk", "<cmd>Telescope keymaps<cr>", desc = "telescope find keymap" },
  { "<leader>fp", "<cmd>Telescope terms<cr>", desc = "telescope pick hidden terms" },
  { "<leader>fm", "<cmd>Telescope marks<cr>", desc = "telescope find marks" },
  { "<leader><leader>", "<cmd>Telescope buffers<CR>", { desc = "telescope find buffers" } },
  {
    "<leader>ft",
    function()
      require("nvchad.themes").open()
    end,
    desc = "telescope themes",
  },
  { "<leader>fgs", "<cmd>Telescope git_status<cr>", desc = "telescope git status" },
  { "<leader>fgc", "<cmd>Telescope git_commits<cr>", desc = "telescope git commits" },
  -- {
  --   "<leader>fw",
  --   function()
  --     local tb = require "telescope.builtin"
  --     local text = vim.getVisualSelection()
  --     tb.grep_string { search = text }
  --   end,
  --   desc = "telescope visual live grep",
  --   mode = "v",
  -- },
  { "<leader>fw", vim.live_grep_word_under_cursor, desc = "Telescope: Search Word Under Cursor" },
  {
    "<leader>fW",
    "<cmd>Telescope live_grep<CR>",
    desc = "Telescope:: live grep",
  },
  {
    "<leader>fw",
    "<cmd>Telescope grep_string<CR>",
    mode = "v",
    desc = "Search Selected Text",
  },
}

-- terminal
wk.add {
  {
    "<leader>th",
    function()
      require("nvchad.term").new { pos = "sp" }
    end,
    desc = "NewTerm horizontal",
  },
  {
    "<leader>tv",
    function()
      require("nvchad.term").new { pos = "vsp" }
    end,
    desc = "NewTerm vertical",
  },
}

-- nvim (<0.7) use this function to set keymap
-- vim.api.nvim_set_keymap("t", "<Esc><Esc>", [[<C-\><C-n>]], { noremap = true })
-- vim.api.nvim_set_keymap("t", "<A-h>", [[<C-\><C-n><C-w>h]], { noremap = true })
-- vim.api.nvim_set_keymap("t", "<A-j>", [[<C-\><C-n><C-w>j]], { noremap = true })
-- vim.api.nvim_set_keymap("t", "<A-k>", [[<C-\><C-n><C-w>k]], { noremap = true })
-- vim.api.nvim_set_keymap("t", "<A-l>", [[<C-\><C-n><C-w>l]], { noremap = true })

map("t", "<A-h>", [[<C-\><C-n><C-W>h]], { desc = "Terminal window left" })
map("t", "<A-j>", [[<C-\><C-n><C-W>j]], { desc = "Terminal window down" })
map("t", "<A-k>", [[<C-\><C-n><C-W>k]], { desc = "Terminal window up" })
map("t", "<A-l>", [[<C-\><C-n><C-W>l]], { desc = "Terminal window right" })
map("t", "<Esc>", [[<C-\><C-n>]], { desc = "Terminal normal mode" })

-- oversear 懒加载激活入口

map("n", "<leader>oa", "<Cmd>OverseerToggle! right<CR>", { desc = "task open oversear" })

-- scroll screen 滚动屏幕

map("n", "gj", "<c-d>", { desc = "half screen down" })
map("n", "gk", "<c-u>", { desc = "half screen up" })
